package com.luck.pf.security.role.web;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.luck.org.entity.SysDepartment;
import com.luck.org.entity.SysMenu;
import com.luck.org.entity.SysRole;
import com.luck.org.entity.VDepartment;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.action.BaseTreeAction;
import com.luck.pf.security.dept.logic.IDeptLogic;
import com.luck.pf.security.dept.qry.DeptQry;
import com.luck.pf.security.menu.logic.IMenuLogic;
import com.luck.pf.security.menu.qry.MenuQry;
import com.luck.pf.security.role.logic.IRoleLogic;
import com.luck.pf.security.role.qry.RoleQry;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.ObjectUtil;
import com.luck.pf.util.Page;

/**
 * 说明:
 * Copyright: Copyright (c) 2010
 * @author zhangwr 2011-8-11 上午09:40:33
 * @version 1.0
 */
public class RoleAction extends BaseTreeAction {
	private static final long serialVersionUID = -475740599412890567L;
	IDeptLogic deptLogic;
	IRoleLogic roleLogic;
	IMenuLogic menuLogic;
	RoleQry qry;
	DeptQry deptQry;
	MenuQry menuQry;
	SysRole entity;
	List<SysRole> roles;
	List<SysDepartment> depts;
	Page page;
	String menuIds;
	String html;
	List<SysMenu> menus,roleMenus;
	public String index() throws Exception{
		return "index";
	}
	public String tree() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			deptQry = (DeptQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (deptQry == null) {
			deptQry = new DeptQry();
		}
		deptQry.setUserInfo(userBean);
		depts = deptLogic.findDepts(deptQry);
		super.title="机构树";
		return "tree";
	}
	/**
	 * 传入roleId,得到此角色的所有权限列表，查出所有菜单，跳到菜单选择页面
	 * @return
	 * @throws Exception
	 */
	public String select() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			menuQry = (MenuQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (menuQry == null) {
			menuQry = new MenuQry();
		}
		menuQry.setUserInfo(userBean);
		menus = menuLogic.findAuthMenu(menuQry);
		/*List dd = new ArrayList();
		for(SysMenu menu:menus){
			TreeVo vo = new TreeVo();
			vo.setTreeId(menu.getId());
			vo.setParentId(menu.getParentId()!=null?menu.getParentId():null);
			vo.setTreeName(menu.getMenuName());
			vo.setTreeNo(menu.getTreeNo());
			dd.add(vo);
		}
		super.treeList = dd;
		html = super.getXmlTree();*/
		roleMenus = menuLogic.findAuthByRoleId(entity.getId());
		for(SysMenu menu:menus){
			for(SysMenu son:roleMenus){
				if(menu.getId().equals(son.getId()))
					menu.setCheck(true);
			}
		}
		super.title="菜单树";
		return "select";
	}
	/**
	 * 根据传入的roleId,菜单ID数据，保存此角色的权限
	 * @return
	 * @throws Exception
	 */
	public String auth() throws Exception{
		entity = roleLogic.get(SysRole.class, entity.getId());
		String str = roleLogic.saveRoleAuth(entity, menuIds);
		if(StringUtils.isBlank(str))
			super.renderJsonSuccess("保存成功");
		else
			super.renderJsonError("保存失败");
		return null;
	}
	public String list() throws Exception{
		UserBean userBean = super.getUserInfo();
		if (StringUtils.isNotBlank(super.qryHex)) {
			qry = (RoleQry)ObjectUtil.getObjectFromHex(qryHex);
		}
		if (qry == null) {
			qry = new RoleQry();
		}
		qry.setUserInfo(userBean);
		if(qry.getDeptId()!=null&&StringUtils.isBlank(qry.getDeptName())){
			qry.setDeptName(roleLogic.get(VDepartment.class, qry.getDeptId()).getDeptName());
		}
		page = roleLogic.findRolePage(qry);
		super.title="角色列表";
		return "list";
	}
	public String toAdd() throws Exception{
		entity = new SysRole();
		entity.setId(ADD_PK);
		super.title="增加角色 ";
		return "edit";
	}
	public String toEdit() throws Exception{
		entity = roleLogic.get(SysRole.class, entity.getId());
		super.title="修改角色";
		return "edit";
	}
	public String edit() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity.setDeptId(userBean.getDeptId());
		entity.setOperTime(DateUtil.getDate());
		entity.setOperUser(userBean.getUserId());
		//entity.setRoleLevel(userBean.getDept().getDeptLevel());
		if(entity.getId() == ADD_PK){//新增
			entity.setId(null);
			roleLogic.saveRole(entity,userBean);
		}else{//修改
			SysRole db = roleLogic.get(SysRole.class, entity.getId());
			//String roleLevel = db.getRoleLevel();
			super.copyProperties(db, entity);
			//db.setRoleLevel(roleLevel);
			roleLogic.saveRole(db,userBean);
		}
		super.redirectUrl = "/security/role_list.action";
		return GLOBAL_SUCCESS;
	}
	public String delete() throws Exception{
		UserBean userBean = super.getUserInfo();
		entity = roleLogic.get(SysRole.class, entity.getId());
		String str = "";
		try {
			str = roleLogic.deleteRole(entity,userBean, createLog(userBean, "删除角色["+entity.getDept().getDeptName()+"][" + entity.getRoleName()+"]", "2"));
		} catch (Exception e) {
			str = e.getMessage();
		}
		if(StringUtils.isNotBlank(str))
			super.renderJsonError(str+"不能删除");
		else
			super.renderJsonSuccess(str);
		return null;
	}
	public String view() throws Exception{
		entity = roleLogic.get(SysRole.class, entity.getId());
		return "view";
	}
	public IRoleLogic getRoleLogic() {
		return roleLogic;
	}
	public void setRoleLogic(IRoleLogic roleLogic) {
		this.roleLogic = roleLogic;
	}
	public RoleQry getQry() {
		return qry;
	}
	public void setQry(RoleQry qry) {
		this.qry = qry;
	}
	public SysRole getEntity() {
		return entity;
	}
	public void setEntity(SysRole entity) {
		this.entity = entity;
	}
	public List<SysRole> getRoles() {
		return roles;
	}
	public void setRoles(List<SysRole> roles) {
		this.roles = roles;
	}
	public IDeptLogic getDeptLogic() {
		return deptLogic;
	}
	public void setDeptLogic(IDeptLogic deptLogic) {
		this.deptLogic = deptLogic;
	}
	public DeptQry getDeptQry() {
		return deptQry;
	}
	public void setDeptQry(DeptQry deptQry) {
		this.deptQry = deptQry;
	}
	public List<SysDepartment> getDepts() {
		return depts;
	}
	public void setDepts(List<SysDepartment> depts) {
		this.depts = depts;
	}
	public Page getPage() {
		return page;
	}
	public void setPage(Page page) {
		this.page = page;
	}
	public IMenuLogic getMenuLogic() {
		return menuLogic;
	}
	public void setMenuLogic(IMenuLogic menuLogic) {
		this.menuLogic = menuLogic;
	}

	public String getMenuIds() {
		return menuIds;
	}
	public void setMenuIds(String menuIds) {
		this.menuIds = menuIds;
	}
	public MenuQry getMenuQry() {
		return menuQry;
	}
	public void setMenuQry(MenuQry menuQry) {
		this.menuQry = menuQry;
	}

	public String getHtml() {
		return html;
	}
	public void setHtml(String html) {
		this.html = html;
	}
	public List<SysMenu> getMenus() {
		return menus;
	}
	public void setMenus(List<SysMenu> menus) {
		this.menus = menus;
	}
	public List<SysMenu> getRoleMenus() {
		return roleMenus;
	}
	public void setRoleMenus(List<SysMenu> roleMenus) {
		this.roleMenus = roleMenus;
	}
	
}
